package com.alibaba.shopping_category_service.service;

import com.alibaba.shopping_category_service.mapper.CategoryMapper;
import com.alibaba.shopping_common.pojo.Category;
import com.alibaba.shopping_common.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName CategroyServiceImpl
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/11 09:02
 */
@DubboService
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增广告信息
     * @param category
     */
    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        // 更新Redis中的数据
        refreshRedisCategory();
    }

    /**
     * 批量删除
     * @param ids 广告ID；列表
     */
    @Override
    public void delete(Long[] ids) {
        categoryMapper.deleteBatchIds(Arrays.asList(ids));
        // 更新Redis中的数据
        refreshRedisCategory();
    }

    /**
     * 更新广告信息
     * @param category
     */
    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        // 更新Redis中的数据
        refreshRedisCategory();
    }

    /**
     * 更新广告的状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        // 1. 先查询Category
        Category category = categoryMapper.selectById(id);
        // 2. 更新字段
        category.setStatus(status);
        // 3. 重新更新
        categoryMapper.updateById(category);
        // 更新Redis中的数据
        refreshRedisCategory();
    }

    @Override
    public Category findById(Long id) {
        return categoryMapper.selectById(id);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Category> search(int page, int size) {
        Page<Category> page1 = new Page<>(page, size);
        return categoryMapper.selectPage(page1,null);
    }

    /**
     * 查询出所有有效的广告信息
     * @return
     */
    @Override
    public List<Category> findAll() {
        ListOperations<String,Category> listOperations = redisTemplate.opsForList();
        List<Category> categoryList = listOperations.range("categories", 0, -1);
        // 判断
        if (categoryList !=null && !categoryList.isEmpty()){
            System.out.println("从Redis中查询广告成功,categories:"+categoryList);
            return categoryList;
        }else {
            // 从数据库实时查询数据
            System.out.println("从数据库实时查询数据");
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
            wrapper.eq("status",1);
            List<Category> categories = categoryMapper.selectList(wrapper);
            // 将查询结果写入Redis中
            listOperations.leftPushAll("categories",categories);
            return categories;
        }
    }

    /**
     * 更新Redis中的数据
     */
    public void refreshRedisCategory(){
        // 从数据库查询广告
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<Category> categories = categoryMapper.selectList(wrapper);
        // 删除Redis中的广告数据
        redisTemplate.delete("categories");
        // 向Redis中同步最新的数据
        ListOperations<String,Category> listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll("categories",categories);
    }

}




